﻿Imports System.Windows.Forms
Imports System.Data
Imports System.Data.Sql
Imports System.Data.SqlClient
Imports System.Data.OleDb
Imports Microsoft.Win32

Public Class Customer

    Public Sub cmb_Customer_click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmb_Customer.Click
        cmb_Customer.DroppedDown = True
    End Sub

    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
        If cmb_Customer.Text <> "" Then
            'Check if entry exists on table already
            Try
                Dim SQLResult As Object
                Dim Records_Retrieved As String = 0

                ESBD_Form.OpenSQLConnection()
                Dim connection As New SqlConnection(csb.ToString())
                cmd.Connection = connection
                connection.Open()
                cmd.CommandText = "Select Count(*) from CUST_TB where Name = '" + cmb_Customer.Text + "'"
                SQLResult = cmd.ExecuteScalar
                Records_Retrieved = SQLResult

                If Records_Retrieved = 0 Then
                    connection.Close()
                    Dim decision As DialogResult

                    decision = MsgBox(cmb_Customer.Text + " does not exist in the database." + vbCrLf + vbCrLf + vbCrLf + "Add " + cmb_Customer.Text + " to the database?", MsgBoxStyle.Question Or MsgBoxStyle.OkCancel)

                    If decision = DialogResult.OK Then
                        'Ask for password
                        If Admin_PW_Entered = "Y" Then
                            GoTo PW_Accepted
                        Else
                            Dim Passwd As String
                            Passwd = InputBox("Please enter Administrator password.", "Add Items Password")
                            If Passwd = Admin_PW Then
                                Admin_PW_Entered = "Y"
                                EditRec_PW_Entered = "Y"
                                DelRec_PW_Entered = "Y"
                                GoTo PW_Accepted
                            ElseIf Passwd = "" Then
                                GoTo End_Block
                            Else
                                MsgBox("Sorry the password you entered did not match the password on file." + vbCrLf + "Please check spelling and capitalization and try again.", MsgBoxStyle.Information)
                                GoTo End_Block
                            End If
                        End If

PW_Accepted:
                        'Call Enter New Customer form
                        Customer_Details.TB_Name.Text = cmb_Customer.Text
                        Customer_Details.TB_Add1.Text = ""
                        Customer_Details.TB_Add2.Text = ""
                        Customer_Details.TB_City.Text = ""
                        Customer_Details.TB_Country.Text = ""
                        Customer_Details.TB_Notes.Text = ""
                        Customer_Details.TB_State.Text = ""
                        Customer_Details.TB_Zip.Text = ""
                        Customer_Details.TB_Add1.Select()
                        Customer_Details.ShowDialog()

                        NewRecord_Form.TB_Customer.Text = cmb_Customer.Text
                        Me.DialogResult = System.Windows.Forms.DialogResult.OK
                        Me.Close()
End_Block:
                    Else 'Cancel or window closed
                        'Reload this window
                    End If
                Else
                    'Do Nothing
                    NewRecord_Form.TB_Customer.Text = cmb_Customer.Text
                    Me.DialogResult = System.Windows.Forms.DialogResult.OK
                    connection.Close()
                End If
            Catch ex As Exception
                Dim connection As New SqlConnection(csb.ToString())
                connection.Close()
                MsgBox(ex.message)
            End Try
        Else
            Me.DialogResult = System.Windows.Forms.DialogResult.OK
        End If
    End Sub

    Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub

    Private Sub Customer_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.cmb_Customer.Select()
    End Sub

    Private Sub BTN_Previous_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BTN_Previous.Click
        Customer_Prev = "Y"
        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub

    Private Sub BTN_Skip_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BTN_Skip.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.OK
    End Sub
End Class
